#include "Gui_functions.h"

void Save_workspace_f(QString name,QString *message, Ui::SymphonieClass Gui,MainConfig *myConfig)
{
    QList<QListWidgetItem *>  mylist;
    QString temp;


    QFile file(name);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        message->append( "failed to open the file, please check the file");
        return ;
    }

    QTextStream out(&file);

    Gui.Ledit_Current_Workspace->setText(myConfig->Current_WorkSpace);

    temp = "#_Symphonie_Workspace";
    out <<temp.toAscii()<< endl;

    temp.clear();
    QTextStream(&temp) <<"# " << QApplication::applicationName() << " " << QApplication::applicationVersion();
    out <<temp.toAscii()<<endl;

    temp.clear();
    QDateTime dateTime = QDateTime::currentDateTime();
    QTextStream(&temp) << "# " << dateTime.toString();
    out <<temp.toAscii()<<endl;

    temp.clear();
    temp = "#_Files_Listing";
    out <<temp.toAscii()<<endl;
    Gui.selected_file->selectAll();
    mylist << Gui.selected_file->selectedItems();
    out << mylist.count()<<endl;
    for(int i=0;i<mylist.count();i++)
    {
        temp = mylist.at(i)->text().toAscii();
        out << temp<< endl;
    }

    temp.clear();
    temp = "#_CONFIG_SPL_OPTIM_TAB";
    out <<temp.toAscii()<<endl;
    out << myConfig->SPL.Optim_Computed_on<<endl;
    out << myConfig->SPL.Optim_target<<endl;
    out << myConfig->SPL.Optim_paramlist.count()<<endl;
    for(int i=0;i<myConfig->SPL.Optim_paramlist.count();i++)
    {
        //  temp = mylist.at(i)->text().toAscii();
        out << myConfig->SPL.Optim_paramlist[i]<< endl;
    }
    out << myConfig->SPL.Optim_paramlist2.count()<<endl;
    for(int i=0;i<myConfig->SPL.Optim_paramlist2.count();i++)
    {
        //  temp = mylist.at(i)->text().toAscii();
        out << myConfig->SPL.Optim_paramlist2[i]<< endl;
    }

    Gui.SPL_exract_results->selectAll();
    mylist.clear();
    mylist << Gui.SPL_exract_results->selectedItems();
    out << mylist.count()<<endl;
    for(int i=0;i<mylist.count();i++)
    {
        temp = mylist.at(i)->text().toAscii();
        out << temp<< endl;
    }

    file.close();

    Gui.selected_file->setCurrentRow(0);
    return ;

}

void Open_workspace_f(QString name,QString *message, Ui::SymphonieClass Gui,MainConfig *myConfig)
{
    QString openFiles;
    QString openFilesPath;
    QList<QListWidgetItem *>  mylist;
    QString temp, line;
    QString y = "\\";
    QFileDialog::Options options;
    int nb;
    options |= QFileDialog::DontUseNativeDialog;
    QString selectedFilter;

    QFile file(name);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        temp = "failed to open the file, please check the file";
        qDebug(temp.toAscii());
        message->append(temp.toAscii());
        return ;
    }

    QTextStream in(&file);
    line = in.readLine();
    qDebug(line.toAscii());

    if( !line.contains("#_Symphonie_Workspace"))
    {
        temp = "this file is not recognized as a Symphonie Workspace, please check the format";
        qDebug(temp.toAscii());
        message->prepend(temp.toAscii());

        return ;
    }
    //clear Gui
    Gui.selected_file->clear();
    Gui.SPL_exract_results->clear();
    Gui.Ledit_Current_Workspace->setText(myConfig->Current_WorkSpace);

    line = in.readLine();// skip soft version
    line = in.readLine();// skip datetime
    while (!line.isNull()) {
        line = in.readLine();
        if(line.contains( "#_Files_Listing"))
        {
            nb = in.readLine().toInt();
            for(int i=0;i<nb;i++){
                line = in.readLine();
                Gui.selected_file->addItem(line);
            }

        }
        if(line.contains( "#_CONFIG_SPL_OPTIM_TAB"))
        {
            myConfig->SPL.Optim_Computed_on =in.readLine();
            myConfig->SPL.Optim_target =in.readLine();
            nb = in.readLine().toInt();
            for(int i=0;i<nb;i++)
            {
                line = in.readLine();
                myConfig->SPL.Optim_paramlist.append(line);
            }
            nb = in.readLine().toInt();
            for(int i=0;i<nb;i++)
            {
                line = in.readLine();
                myConfig->SPL.Optim_paramlist2.append(line);
            }
            nb = in.readLine().toInt();
            for(int i=0;i<nb;i++){
                line = in.readLine();
                Gui.SPL_exract_results->addItem(line);
            }

        }
    }
    file.close();
    return ;
}

int find_index_QListString(QString param, QList<swp_rec*> mylist)
{
    int i=0;
    QString temp;
    temp = mylist.at(i)->Label;
    while(!temp.contains(param)&& i <  mylist.count())
        {
            temp = mylist.at(i)->Label;
            i++;
        }
    if(!temp.contains(param))return -1;
    if(i==0) return 0;

    return i-1;
}

int find_index_Freq_and_Vars(QString param,float freq, QList<Freq_and_Vars> mylist)
{
    int i=0;
    QString temp;
    float temp_f;
    temp_f = mylist.at(0).freq;
    while(temp_f !=freq && i <mylist.count() )
    {
        temp_f = mylist.at(i).freq;
        i++;
    }


    if(temp_f !=freq){
        temp.clear();
        QTextStream(&temp) << "error :temp_f !=freq " <<temp_f << " " <<freq;
        qDebug(temp.toAscii());
        return -1;
    }
    if( i>0) i= i-1;
    if(param.contains("freq"))return i;
    int j=0;
    temp = mylist.at(i).freqvar.at(j).Label;

    while(!temp.contains(param)&& j <  mylist.at(i).freqvar.count())
        {
            temp = mylist.at(i).freqvar.at(j).Label;
            j++;
        }
    if(!temp.contains(param)){
        temp.clear();
        QTextStream(&temp) << "error :!temp.contains(param):  " << param.toAscii();
        qDebug(temp.toAscii());
        return -1;
    }
    if(j>0) j = j-1;

    return mylist.at(i).freqvar.at(j).var;
}
/*
qreal myMin(QVector<qreal> x)
{
    qreal min = x.at(0);
    for(int i=0;i<x.count();i++)
        if(x.at(i)<min) min = x.at(i);
    return min;
}

qreal myMax(QVector<qreal> x)
{
    qreal max = x.at(0);
    for(int i=0;i<x.count();i++)
        if(x.at(i)>max) max = x.at(i);
    return max;
}
*/
void print2DebugFile(QString text)
{
    QFile file("debug.txt");
     if (!file.open(QIODevice::Append | QIODevice::Text))
         return;

     QTextStream in(&file);

         in << text.toAscii() <<endl;
         file.close();

}
